﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class Edit_Order_AddressController : UserController
    {
        public IOrdersService _orders { get; set; }
        [Authorize]
        [HttpGet]
        public IActionResult Get(int order_id,string accept_name, string province, string city, string area, string address, string mobile,string post_code)
        {
            //检查用户是否登录
            Users userModel = GetUserInfo();
            if (userModel == null)
            {
                return Ok(new { status=0,msg= "用户尚未登录或已超时！" });
            }
            //获取传参信息===================================
            string telphone = mobile;
            string email = string.Empty;
            //检查收货人
            if (string.IsNullOrEmpty(accept_name))
            {
                return Ok(new { status = 0, msg = "请输入收货人姓名！" });
            }
            //检查手机和电话
            if (string.IsNullOrEmpty(telphone) && string.IsNullOrEmpty(mobile))
            {
                return Ok(new { status = 0, msg = "请输入收货人联系手机！" });
            }
            //检查地区
            if (string.IsNullOrEmpty(province) && string.IsNullOrEmpty(city))
            {
                return Ok(new { status = 0, msg = "请选择您所在的省市区！" });
            }
            //检查地址
            if (string.IsNullOrEmpty(address))
            {
                return Ok(new { status = 0, msg = "请输入详细的收货地址！" });
            }
            var order = _orders.GetModel(order_id);
            if (order == null)
            {
                return Ok(new { status = 0, msg = "订单不存在！" });
            }            
            if (order.user_id != userModel.id)
            {
                return Ok(new { status = 0, msg = "不是自己的订单！" });
            }
            //修改订单=======================================        
            order.accept_name = accept_name;
            order.area = province + "," + city + "," + area; //省市区以逗号相隔
            order.address = address;
            order.telphone = telphone;
            order.mobile = mobile;
            order.email = email;
            order.post_code = post_code;
            string updateStr = "accept_name='" + order.accept_name + "',area='" + order.area + "',address='" + order.address + "',telphone='" + order.telphone + "',mobile='" + order.mobile + "',email='" + order.email + "',post_code='" + order.post_code + "'";
            _orders.UpdateField(order.id, updateStr);
            return Ok(new { status = 1, msg = "订单已成功修改！" });
        }
    }
}